/*
 * @Date: 2022-03-04 11:07:19
 * @LastEditors: 春贰
 * @gitee: https://gitee.com/chun22222222
 * @github: https://github.com/chun222
 * @Desc: 
 * @LastEditTime: 2022-05-19 10:21:04
 * @FilePath: \web\src\composable\tab.js
 */
import { ref, watch, computed } from "vue";
import { useRoute, useRouter } from "vue-router";
import store from "@/core/store"
import {uuid} from "@/core/tools/common"

export function useTab() {
    const route = useRoute();
    const router = useRouter();
    const tabs = computed(() => store.getters.tabs);
    watch(() => route.path, (path) => {
        const title = route.meta.title;
        const query = route.query;
        add({ title, path, query });
        // if (route.meta.isshowmenu==1) {
        //     add({ title, path })
        // } 
    }, { immediate: true }) 

    function add(tab) { 
        const findtab = tabs.value.list.find((item) => item.path === tab.path &&  JSON.stringify(item.query) === JSON.stringify(tab.query));
        if (!findtab) { 
            tabs.value.list.push(tab)
            tab.uuid = uuid();
            tabs.value.active = Object.assign({}, tab);
        }else{
            tabs.value.active = Object.assign({}, findtab);
        }  
        store.commit('user/SET_USER_TABS', tabs.value);
    }

    //路由跳转携带参数
    function to(uuid) {  
        const tab = tabs.value.list.find((item) => item.uuid === uuid ) 
        router.push({ path: tab.path, query: tab.query });  
    }

    //关闭
    function close(uuid) {
        const tab = tabs.value.list.find((item) => item.uuid === uuid ) 
        const index = tabs.value.list.findIndex((item) => item.uuid == uuid);
        //删除数组某一项
        tabs.value.list.splice(index, 1);  
        //关闭的是当前tab
        if (route.path === tab.path && JSON.stringify(route.query) == JSON.stringify(tab.query) && tabs.value.list.length) {
            if (index >= 1) {
                to(tabs.value.list[index - 1].uuid);
                tabs.value.active = Object.assign({}, tabs.value.list[index - 1]);
            } else {
                to(tabs.value.list[index].uuid); 
                tabs.value.active = Object.assign({}, tabs.value.list[index]);
            }
        }
        store.commit('user/SET_USER_TABS', tabs.value);
    }

    function closeCurrent() { 

        if (tabs.value.list.length > 1) { 
           const tab = tabs.value.list.find((item) => item.path === route.path &&  JSON.stringify(item.query) === JSON.stringify(route.query))
            close(tab.uuid);
        }
    }

    function closeOther() { 
        tabs.value.list = [tabs.value.active];
        store.commit('user/SET_USER_TABS', tabs.value);
    }

    //关闭客户端所有标签
    function closeClient() { 
        //过滤
        console.log("closeClient",tabs.value.list);
        tabs.value.list = tabs.value.list.filter((item) => item.path.indexOf('mes_client') == -1); 
        store.commit('user/SET_USER_TABS', tabs.value);
    }

    return {
        add,
        to,
        close,
        closeOther,
        closeCurrent,
        closeClient,
    }

}